package com.leather.question.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.leather.common.entity.Question;
import com.leather.question.vo.InviteVo;
import com.leather.question.vo.QuestionQueryVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author kevin
 */
public interface QuestionMapper extends BaseMapper<Question> {


    /**
     * 分页获取所有数据
     *
     * @param pageId
     * @return
     */
    @Select("select a.*,b.nick_name,b.img_url from question a,user b\n" +
            "where a.user_id = b.id order by id desc limit #{pageId},15")
    List<QuestionQueryVo> index(@Param("pageId") int pageId);


    @Update("update question set stars = stars + 1 where id = #{questionId}")
    int star(int questionId);


    @Delete("delete from question where id = #{questionId} and  user_id = #{userId}")
    int deleteQuestion(@Param("questionId") int questionId, @Param("userId") int userId);

    @Update("update question set answers =  answers + 1 where id = #{questionId}")
    int answerAdd(int questionId);

    @Update("update question set answers =  answers - 1 where id = #{questionId}")
    int answerRemove(int questionId);


    /**
     * 根据questionId获取Question内容
     *
     * @param questionId
     * @return
     */
    @Select("select a.*,b.nick_name,b.img_url from question a,user b\n" +
            "where a.user_id = b.id and a.id = #{questionId}")
    QuestionQueryVo getByQuestionId(@Param("questionId") int questionId);


    @Select("select  id,img_url,nick_name from user \n" +
            "where id not in (\n" +
            " select invite_id from invite where question_id = #{questionId}\n" +
            " and user_id = #{userId}\n" +
            ") order by #{order} limit  4")
    List<InviteVo> inviteUser(@Param("questionId") int questionId, @Param("userId") int userId, @Param("order") String order);


    @Insert("insert into invite(user_id,invite_id,question_id) values(#{userId},#{inviteId},#{questionId})")
    int insertInvite(@Param("userId") int userId, @Param("inviteId") int inviteId, @Param("questionId") int questionId);

}